//
//  NovelViewController.m
//  Read
//
//  Created by lanou on 15/12/17.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "NovelViewController.h"
#import "NovelCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "ListNovelCollectionViewCell.h"
#import "ListNovel2CollectionViewCell.h"
#import "Novel.h"

@interface NovelViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *topArray;//分区1的图片

@property (nonatomic, retain) NSArray *titleArray;//分区头标题

@property (nonatomic, retain) NSArray *sectionArray;//分区类标题

@property (nonatomic, retain) NSMutableArray *dataArray;//总的数据

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSArray *smallArray;//第一个分区的数据

@property (nonatomic, retain) NSMutableArray *oneArray;//每个分区数据的前三个



@end

@implementation NovelViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (NSMutableArray *)oneArray {
    if (!_oneArray) {
        _oneArray = [[NSMutableArray alloc] init];
    }
    return _oneArray;
}


- (void)setSectionData2 {
    
    _smallArray = @[@"8", @"7", @"3", @"4", @"1"];
    NSArray *smallArray = @[@"1", @"2", @"4", @"0", @"0"];
    
    for (int i = 0; i < self.smallArray.count; i ++) {
        NSMutableArray *rootArray = [[NSMutableArray alloc]init];
        
        NSMutableArray *rootArray1 = [[NSMutableArray alloc] init];//每个数据只有前六个数据
        [LORequestManger GET:[NSString stringWithFormat:kDataUrlOne,self.smallArray[i],smallArray[i]] success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            NSArray *array = dic[@"item"];
            
            NSLog(@"%d",i);
            for (NSDictionary *dic1 in array) {
                Novel *novel = [[Novel alloc] init];
                [novel setValuesForKeysWithDictionary:dic1];
                [rootArray addObject:novel];
            }
            
            //每个数组前六个
            for (int i = 4; i < 10; i ++) {
                [rootArray1 addObject:rootArray[i]];
            }
            
            
            [self.collectionView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error");
        }];
        
        [self.oneArray addObject:rootArray1];
        [self.dataArray addObject:rootArray];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topArray = [NSMutableArray array];
    //分区图片
    for (int i = 0; i < 3; i ++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
        [self.topArray addObject:image];
    }
    [self.oneArray addObject:self.topArray];
    
     self.smallArray = [NSMutableArray arrayWithCapacity:3];
    [self setSectionData2];
    
    BlurImageView *blurImage = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:blurImage];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //列距（同一行中元素之间的距离）
    flowLayout.minimumInteritemSpacing = 5;
    
    //行距
    flowLayout.minimumLineSpacing = 20;
    
    //分区内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    CGFloat totalWidth = self.view.frame.size.width;
    
    CGFloat itemWidth = (totalWidth - 2 * 5 - 2 * 5) / 3.0;
    CGFloat itemHeight = 1.5 * itemWidth;
    
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
    
    //滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    [_collectionView setBackgroundView:blurImage];
    
    //设置 数据源 和 代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [_collectionView registerClass:[NovelCollectionViewCell class] forCellWithReuseIdentifier:@"firstCell"];
    [_collectionView registerClass:[ListNovelCollectionViewCell class] forCellWithReuseIdentifier:@"twoCell"];
    [_collectionView registerClass:[ListNovel2CollectionViewCell class] forCellWithReuseIdentifier:@"thirdCell"];
    
    //区头
    [_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1"];
    
    //添加
    [self.view addSubview:_collectionView];
    
   
    
    
    //分区头标题
    self.titleArray = @[@"排行榜", @"分类", @"新书榜"];
    self.sectionArray = @[@"好书推荐", @"畅销榜", @"周周好书", @"月排行", @"百度热词榜"];
    
    
    
    
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.oneArray.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [self.oneArray[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NovelCollectionViewCell *topCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        topCell.pictureView.image = self.oneArray[indexPath.section][indexPath.item];
        topCell.titleLabel.text = self.titleArray[indexPath.item];
        topCell.backgroundColor = [UIColor whiteColor];
        return topCell;
    } else if (indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 3) {
        ListNovelCollectionViewCell *listCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"twoCell" forIndexPath:indexPath];
        Novel *novel = [[Novel alloc]init];
        novel = [self.oneArray[indexPath.section] objectAtIndex:indexPath.item];
        
        NSLog(@"%@",novel.author);
        listCell.novel = novel;
        return listCell;
    } else {
        ListNovel2CollectionViewCell *listCell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"thirdCell" forIndexPath:indexPath];
        listCell2.novel = self.oneArray[indexPath.section][indexPath.item];
        return listCell2;
    }
}

//分区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section != 0) {
        HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1" forIndexPath:indexPath];
        
        //更多图书
        [header.moreButton addTarget:self action:@selector(moreBook) forControlEvents:(UIControlEventTouchUpInside)];
        header.titleLabel.text = self.sectionArray[indexPath.section - 1];
        return header;
    } else {
        return nil;
    }
    
}

//分区头大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(0, 0);
    } else {
        return CGSizeMake(0, 50);
    }
    
    
}

//区尾
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    
    if (section != 5) {
        return CGSizeMake(0, 0);
    } else {
        return CGSizeMake(0, 120);
    }
    
}

//列距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return 0;
    } else {
        return 0;
    }
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return UIEdgeInsetsMake(10, 0, 10, 0);
    } else {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(125, 150);
    } else if (indexPath.section == 4 || indexPath.section == 5) {
        return CGSizeMake(kWidth - 20, 150);
    } else {
        return CGSizeMake(100, 150);
    }
    
}


#pragma mark -------------区头more按钮的点击事件-------

- (void)moreBook {
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
